package js.web.center.thread;

import java.util.ArrayList;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import js.web.center.core.config.JsWebConfig;
import js.web.center.utils.JsWebSyncCtrlUtils;

/**
 * web sesssion管理线程
 * @author qpduwei
 *
 */
public class JsWebSessionThread implements Runnable {
	private Logger logger=Logger.getLogger(JsWebSessionThread.class);
	@Override
	public void run() {
		while(true){
			try{
				ArrayList<String> delSessionIds=new ArrayList<>();
				ArrayList<Entry<String,Long>> allSessionIds = JsWebSyncCtrlUtils.getAllSessionIds();
				for (Entry<String, Long> entry : allSessionIds) {
					if(System.currentTimeMillis()-entry.getValue()>JsWebConfig.webSessionOutTime){
						delSessionIds.add(entry.getKey());
					}
				}
				for (int i = 0; i < delSessionIds.size(); i++) {
					JsWebSyncCtrlUtils.removeSessionId(delSessionIds.get(i));
				}
			}catch(Exception e){
				logger.error("JsWebSessionThread ERROR:{}",e);
			}
			try {
				Thread.sleep(JsWebConfig.getSessionUpdateTime);
			} catch (InterruptedException e) {
				logger.error("JsWebSessionThread ERROR:{}",e);
			}
		}
	}
}